package com.weiyun.notebook

import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.weiyun.notebook.Database.AppDatabase
import com.weiyun.notebook.Entity.Note
import kotlinx.android.synthetic.main.activity_add.*
import java.text.SimpleDateFormat
import java.util.*

class AddActivity : AppCompatActivity() {
    private lateinit var noteViewModel: NoteViewModel

    //    val Dao = AppDatabase.getDatabase(this).NoteDao()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add)
        noteViewModel = ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(application)
        ).get(NoteViewModel::class.java)
    }

    fun add(view: View?) {
        val title = etAdd_title!!.text.toString()
        val content = etAdd_content!!.text.toString()
        if (TextUtils.isEmpty(title)) {
            ToastUtil.toastShort(this, "标题不能为空")
            return
        }
        val note=Note(title=title, createTime = getCurrentTimeFormat(), content = content)
        noteViewModel.insert(note)
        ToastUtil.toastShort(this, "添加成功！")
        finish()
    }
    private fun getCurrentTimeFormat(): String? {
        var simpleDateFormat: SimpleDateFormat? = null
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            simpleDateFormat = SimpleDateFormat("YYYY年MM月dd日 HH:mm:ss")
        }
        val date = Date()
        return simpleDateFormat!!.format(date)
    }
}